package gui;

import fs.Data;
import fs.EmailConfig;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

/**
 *
 * @author lucifer
 */
public class EmailConfigDialog extends javax.swing.JDialog {

  public static final int RET_CANCEL = 0;
  public static final int RET_OK = 1;

  private Frame parent;
  private EmailConfig e;

  public EmailConfigDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    this.parent = parent;
    initComponents();

    initConfig();

    String cancelName = "cancel";
    InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
    ActionMap actionMap = getRootPane().getActionMap();
    actionMap.put(cancelName, new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        doClose(RET_CANCEL);
      }
    });
  }

	private void initConfig() {
    e = new EmailConfig("clinica.senzu", "love");
		try {
      e = (EmailConfig) Data.ReadFrom(Data.EMAIL_SETTINGS);
		}
		catch (Exception e) {
		}
    addrjTextField.setText(e.Addr);
    passwdjPasswordField.setText(e.Passwd);
	}

  private void setDefaults() {
    addrjTextField.setText("clinica.senzu");
    passwdjPasswordField.setText("love");
  }

  @SuppressWarnings("deprecation")
  private void saveConfig() {
    e.Addr = addrjTextField.getText();
    e.Passwd = passwdjPasswordField.getText();
    
    try {
      Data.WriteObjectTo(e, Data.EMAIL_SETTINGS);
    } catch (Exception e) {
      System.out.printf("?can't save email config\n");
    }

    JOptionPane.showMessageDialog(parent, "Configuración del email guardada. La configuración de la cuenta de email se actualizó correctamente.", "Éxito!", JOptionPane.INFORMATION_MESSAGE);
  }

  public int getReturnStatus() {
    return returnStatus;
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    okButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();
    addrjLabel = new javax.swing.JLabel();
    addrjTextField = new javax.swing.JTextField();
    passwdjLabel = new javax.swing.JLabel();
    passwdjPasswordField = new javax.swing.JPasswordField();
    defaultjButton = new javax.swing.JButton();
    gmailjTextField = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Configuración de la Cuenta de Email");
    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        closeDialog(evt);
      }
    });

    okButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/aceptar.png"))); // NOI18N
    okButton.setText("Aceptar");
    okButton.setMaximumSize(new java.awt.Dimension(120, 27));
    okButton.setMinimumSize(new java.awt.Dimension(120, 27));
    okButton.setPreferredSize(new java.awt.Dimension(120, 27));
    okButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okButtonActionPerformed(evt);
      }
    });

    cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/cancelar.png"))); // NOI18N
    cancelButton.setText("Cancelar");
    cancelButton.setMaximumSize(new java.awt.Dimension(120, 27));
    cancelButton.setMinimumSize(new java.awt.Dimension(120, 27));
    cancelButton.setPreferredSize(new java.awt.Dimension(120, 27));
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelButtonActionPerformed(evt);
      }
    });

    addrjLabel.setText("Email");

    addrjTextField.setMaximumSize(new java.awt.Dimension(153, 27));
    addrjTextField.setMinimumSize(new java.awt.Dimension(153, 27));
    addrjTextField.setPreferredSize(new java.awt.Dimension(153, 27));
    addrjTextField.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        addrjTextFieldFocusGained(evt);
      }
    });

    passwdjLabel.setText("Password");

    passwdjPasswordField.setMaximumSize(new java.awt.Dimension(255, 27));
    passwdjPasswordField.setMinimumSize(new java.awt.Dimension(255, 27));
    passwdjPasswordField.setPreferredSize(new java.awt.Dimension(255, 27));
    passwdjPasswordField.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        passwdjPasswordFieldFocusGained(evt);
      }
    });

    defaultjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/defaults.png"))); // NOI18N
    defaultjButton.setText("Defaults");
    defaultjButton.setMaximumSize(new java.awt.Dimension(120, 27));
    defaultjButton.setMinimumSize(new java.awt.Dimension(120, 27));
    defaultjButton.setPreferredSize(new java.awt.Dimension(120, 27));
    defaultjButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        defaultjButtonActionPerformed(evt);
      }
    });

    gmailjTextField.setEditable(false);
    gmailjTextField.setText("@gmail.com");
    gmailjTextField.setEnabled(false);
    gmailjTextField.setMaximumSize(new java.awt.Dimension(96, 27));
    gmailjTextField.setMinimumSize(new java.awt.Dimension(96, 27));
    gmailjTextField.setPreferredSize(new java.awt.Dimension(96, 27));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(passwdjLabel, javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(addrjLabel, javax.swing.GroupLayout.Alignment.TRAILING))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(addrjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(gmailjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
              .addComponent(passwdjPasswordField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(defaultjButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );

    layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGap(12, 12, 12)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(addrjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(addrjLabel)
          .addComponent(gmailjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(passwdjPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(passwdjLabel))
        .addGap(18, 18, 18)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(defaultjButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
    );

    getRootPane().setDefaultButton(okButton);

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
      doClose(RET_OK);
      saveConfig();
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
      doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
      doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

  private void defaultjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultjButtonActionPerformed
    setDefaults();
  }//GEN-LAST:event_defaultjButtonActionPerformed

  private void addrjTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_addrjTextFieldFocusGained
    addrjTextField.selectAll();
  }//GEN-LAST:event_addrjTextFieldFocusGained

  private void passwdjPasswordFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwdjPasswordFieldFocusGained
    passwdjPasswordField.selectAll();
  }//GEN-LAST:event_passwdjPasswordFieldFocusGained
  
  private void doClose(int retStatus) {
    returnStatus = retStatus;
    setVisible(false);
    dispose();
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel addrjLabel;
  private javax.swing.JTextField addrjTextField;
  private javax.swing.JButton cancelButton;
  private javax.swing.JButton defaultjButton;
  private javax.swing.JTextField gmailjTextField;
  private javax.swing.JButton okButton;
  private javax.swing.JLabel passwdjLabel;
  private javax.swing.JPasswordField passwdjPasswordField;
  // End of variables declaration//GEN-END:variables

  private int returnStatus = RET_CANCEL;
}
